package org.basic.sms.service.impl;

import org.basic.common.core.utils.MapstructUtils;
import org.basic.common.core.utils.StringUtils;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.basic.sms.domain.SysSmsTemplateParam;
import org.basic.sms.domain.bo.SysSmsTemplateParamBo;
import org.basic.sms.domain.vo.SysSmsTemplateParamVo;
import org.basic.sms.mapper.SysSmsTemplateParamMapper;
import org.basic.sms.service.ISysSmsTemplateParamService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 邮件模板参数Service业务层处理
 *
 * @author Shao Hua
 * @date 2023-07-20
 */
@RequiredArgsConstructor
@Service
public class SysSmsTemplateParamServiceImpl implements ISysSmsTemplateParamService {

    private final SysSmsTemplateParamMapper baseMapper;

    /**
     * 查询邮件模板参数
     */
    @Override
    public List<String> queryById(Long id){
        List<SysSmsTemplateParamVo> sysSmsTemplateParamVos = baseMapper.selectVoList(new LambdaQueryWrapper<>(SysSmsTemplateParam.class)
            .eq(SysSmsTemplateParam::getTemplateId, id));
        return sysSmsTemplateParamVos.stream().map(data->{
            String param = "" + data.getParamName();
            return param;
        }).collect(Collectors.toList());
    }

    /**
     * 查询邮件模板参数列表
     */
    @Override
    public TableDataInfo<SysSmsTemplateParamVo> queryPageList(SysSmsTemplateParamBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSmsTemplateParam> lqw = buildQueryWrapper(bo);
        Page<SysSmsTemplateParamVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮件模板参数列表
     */
    @Override
    public List<SysSmsTemplateParamVo> queryList(SysSmsTemplateParamBo bo) {
        LambdaQueryWrapper<SysSmsTemplateParam> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSmsTemplateParam> buildQueryWrapper(SysSmsTemplateParamBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSmsTemplateParam> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getParamName()), SysSmsTemplateParam::getParamName, bo.getParamName());
        lqw.eq(bo.getTemplateId() != null, SysSmsTemplateParam::getTemplateId, bo.getTemplateId());
        return lqw;
    }

    /**
     * 新增邮件模板参数
     */
    @Override
    public Boolean insertByBo(SysSmsTemplateParamBo bo) {
        SysSmsTemplateParam add = MapstructUtils.convert(bo, SysSmsTemplateParam.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件模板参数
     */
    @Override
    public Boolean updateByBo(SysSmsTemplateParamBo bo) {
        SysSmsTemplateParam update = MapstructUtils.convert(bo, SysSmsTemplateParam.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSmsTemplateParam entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件模板参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
